#include "user_bt_common.h"
#include "lvgl.h"
#include "FreeRTOS.h"

#define BT_COMM_ENABLE   1
#if BT_COMM_ENABLE
#define BT_COMM_LOG  printf
#else
#define BT_COMM_LOG
#endif

user_bt_vol_t user_bt_volum;
bool system_bt_module = 0;    //0:close 1:start

/*
    change phone volume to local spk vol
*/
bool    user_bt_set_local_volume(enum bt_volume_type_t type,uint16_t vol)
{

    uint16_t avrcp_vol,hf_vol;

    if((type >= BT_VOL_MAX)||(vol > 0xFFFF)){
        return false;
    }
    if(type == BT_VOL_HFP){

        if(user_bt_env.dev[user_bt_env.last_active_index].conFlags&LINK_STATUS_SCO_CONNECTED){
            if(vol == 0){
                hf_vol = 0;
            }
            else{
                hf_vol = 0 + (USER_BT_HF_VOLUME_MAX*vol)/15;
            }

            BT_COMM_LOG("%s %d %d \r\n",__func__,vol,hf_vol);
            //codec_Set_DAC_Volume(hf_vol);			
        }
    }
    else if(type == BT_VOL_MEDIA){

        if(user_bt_env.dev[user_bt_env.last_active_index].conFlags&LINK_STATUS_MEDIA_PLAYING){
            if(vol >= USER_BT_VOLUM_MAX){
                avrcp_vol = 127;
            }
            else{
                avrcp_vol = (USER_BT_VOLUM_MAX*vol)/127;
            }
            BT_COMM_LOG("%s %d %d \r\n",__func__,vol,avrcp_vol);
            //codec_Set_DAC_Volume(avrcp_vol);            
        }else{

        }

    }
    else if(type == BT_VOL_TONE){

    }

    return true;
}


/*
    local change volume send to phone
*/
bool    user_bt_set_spk_volume(enum bt_volume_type_t type,uint16_t vol)
{
    uint16_t avrcp_vol,hf_vol;

    if((type >= BT_VOL_MAX)||(vol > 0xFFFF)){
        return false;
    }
    if(type == BT_VOL_HFP){

        if(user_bt_env.dev[user_bt_env.last_active_index].conFlags&LINK_STATUS_SCO_CONNECTED){
            if(vol == 0){
                hf_vol = + (USER_BT_HF_VOLUME_MAX * 1)/15;
                vol    = 1;
            }
            else{
                hf_vol = 0 + (USER_BT_HF_VOLUME_MAX*vol)/15;
            }

            BT_COMM_LOG("%s %d %d \r\n",__func__,vol,hf_vol);
            bt_report_spk_volume(user_bt_env.last_active_index,vol);
            //codec_Set_DAC_Volume(hf_vol);			
        }
    }
    else if(type == BT_VOL_MEDIA){

        if(user_bt_env.dev[user_bt_env.last_active_index].conFlags&LINK_STATUS_MEDIA_PLAYING){
            if(vol == 0x3f){
                avrcp_vol = USER_BT_VOLUM_MAX;
            }
            else{
                avrcp_vol = (USER_BT_VOLUM_MAX*vol)/127;
            }
            bt_set_media_volume(user_bt_env.last_active_index, vol);
            //codec_Set_DAC_Volume(avrcp_vol);

        }else{

        }
    }
    else if(type == BT_VOL_TONE){

    }

    return true;
}

user_bt_vol_t *user_bt_get_volume(void)
{
    return (user_bt_vol_t *)&user_bt_volum.vol;
}

uint8_t    user_bt_volume_lev_convert(enum bt_volume_type_t type,uint8_t lev)
{


    uint8_t vol; 

    if(type == BT_VOL_MEDIA)
    {
        vol = (lev * 127 / 100);
    }
    else if(type == BT_VOL_HFP)
    {
        vol = (lev * 15 / 100);
    }
    printf("%s %d \r\n", __func__,vol);
    return vol;   
}

uint8_t *user_bt_get_local_name(void)
{
    extern uint8_t default_name[];
    return default_name;
}
uint8_t *user_bt_get_mac(void)
{
    extern uint8_t bt_addr[];
    return bt_addr;
}
char *user_ble_get_local_name(void)
{
    extern char local_device_name[];
    return local_device_name;
}
uint8_t *user_ble_get_mac(void)
{
    extern uint8_t ble_static_addr[];
    return ble_static_addr;
}

/*
    bt module enable/disable api
    system_bt_module param
    0:close
    1:start
*/
void    user_bt_set_module(bool sat)
{
    system_bt_module = sat;
}

bool    user_bt_get_module(void)
{
    return system_bt_module;
}


void    user_bt_disconnect(void)
{
    BtStatus status;

    status = bt_disconnect(&user_bt_env.dev[0].remote_bd, true);    //强制断开bt
    BT_COMM_LOG("%s %d %d\r\n", __func__,__LINE__,status);
}

void    user_bt_off(void)
{
    BtStatus status;

    status =  bt_disconnect(&user_bt_env.dev[0].remote_bd, true);   //强制断开bt
    BT_COMM_LOG("%s %d %d\r\n", __func__,__LINE__,status);

    status = bt_exit_pairing();

    user_bt_set_module(false);

    BT_COMM_LOG("%s %d %d\r\n", __func__,__LINE__,status);    
}


void    user_bt_on(void)
{
    BtStatus status;

    BtAccessModeInfo access_mode_nc = {
        .inqInterval    = 0x800,
        .inqWindow      = 0x12,
        .pageInterval   = 0x800,
        .pageWindow     = 0x12,
    };

    status = bt_enter_pairing(BAM_GENERAL_ACCESSIBLE, &access_mode_nc);

    user_bt_env.connect_times = 3;  
    BT_COMM_LOG("bt reconnect addr %x %x %x %x %x %x \r\n",user_bt_env.last_dev_addr.A[0],
                                                           user_bt_env.last_dev_addr.A[1],
                                                           user_bt_env.last_dev_addr.A[2],
                                                           user_bt_env.last_dev_addr.A[3],
                                                           user_bt_env.last_dev_addr.A[4],
                                                           user_bt_env.last_dev_addr.A[5]);
    status = bt_connect(&user_bt_env.last_dev_addr,ENABLE_PROFILE_ALL);

    user_bt_set_module(true);
    printf("user_bt_on\r\n");
    BT_COMM_LOG("status = %d\r\n",status);
}




void    user_bt_profile_disable(uint8_t profile)
{
    BtStatus status;
    BT_COMM_LOG("%s profile %d\r\n",__func__,profile);
    switch(profile)
    {
        case ENABLE_PROFILE_HF:
            if(user_bt_env.dev[user_bt_env.last_active_index].hf_chan->state != HF_STATE_CLOSED){
                status = HF_DisconnectServiceLink(user_bt_env.dev[user_bt_env.last_active_index].hf_chan);
                BT_COMM_LOG("status %d\r\n",status);
            }
        break;

        case ENABLE_PROFILE_A2DP_SINK:
            if(user_bt_env.dev[user_bt_env.last_active_index].pstream->stream.state != 0){
                status = A2DP_CloseStream(user_bt_env.dev[user_bt_env.last_active_index].pstream);
                BT_COMM_LOG("status %d\r\n",status);
            }
        break;
    }
}

void    user_bt_profile_enable(uint8_t profile)
{
    BtStatus status;
    BT_COMM_LOG("%s profile %d\r\n",__func__,profile);
    uint8_t index = 0;
    switch(profile)
    {
        case ENABLE_PROFILE_HF:
            if(user_bt_env.enable_profiles & ENABLE_PROFILE_HF){
                    index = bt_get_free_hf_channel();
                    status = HF_CreateServiceLink(&hf_channel[index], &user_bt_env.last_dev_addr);
                    BT_COMM_LOG("status %d\r\n",status);
                }
        break;

        case ENABLE_PROFILE_A2DP_SINK:
            if(user_bt_env.enable_profiles & ENABLE_PROFILE_A2DP_SINK){
                    index = bt_get_free_a2dp_sink_stream();
                    status = A2DP_OpenStream(&Stream[index],&user_bt_env.last_dev_addr);
                    BT_COMM_LOG("status %d\r\n",status);
                }
        break;
    }

}